<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>防抖</title>
  <!-- https://www.bilibili.com/video/BV1pQ4y1M71e/?spm_id_from=333.788.videocard.3 -->
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    :root,
    body {
      height: 100%;
    }

    body {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    div {
      width: 300px;
      height: 200px;
      border: 2px solid pink;
      display: flex;
      align-items: center;
      justify-content: center;
      color: red;
      font-size: 30px;
      cursor: move;
      margin-bottom: 300px;
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  /* 
    事件响应函数在一段时间后才执行，如果在这段时间内容再次调用，则重新计算执行时间，当预定时间内没有调用函数则执行
  */
  "use strict"; {
    const div = document.querySelector("div");
    let count = 0;
    {
      /* div.addEventListener("mousemove", async () => {
        div.innerHTML = count++;
      }); */
    };
    { //防抖函数
      const debounce = function (fun, wait) {
        let tiemout; //定义变量
        return function (event) {
          let content = this;
          clearTimeout(tiemout); //一进来就进行清空处理
          tiemout = setTimeout(() => {
            fun({ content, e });
          }, wait);
        };
      };

      // 处理的函数
      const dosomeing = ({ content = "this指向", event = "事件对象" } = {}) => {
        // console.log(this);
        console.log(`content===s`, content);
        console.log(event);

        div.innerHTML = count++;
      };

      div.addEventListener("mousemove", debounce(dosomeing, 300));
    };
  };
</script>

</html>